using System;
using System.Runtime.InteropServices;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Serialization;
using System.Data;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
using Microsoft.SharePoint.WebPartPages;
using SharePointing.Features.Translation.Common;

namespace SharePointing.Features.Translation
{
    public class SPGridSamplePart : System.Web.UI.WebControls.WebParts.WebPart
    {
        private SPGridView oGrid;
        private SampleData oDataset;
        private DataView oView;
        private TextBox oTextBox = new TextBox();

        private void PopulateDataset()
        {
            oDataset = new SampleData();
            oDataset.Samples.AddSamplesRow(1, "Sample1", "SampleType1");
            oDataset.Samples.AddSamplesRow(2, "Sample2", "SampleType2");
            oDataset.Samples.AddSamplesRow(3, "Sample3", "SampleType3");
            oDataset.Samples.AddSamplesRow(4, "Sample4", "SampleType4");
            oDataset.Samples.AddSamplesRow(5, "Sample5", "SampleType1");
            oDataset.Samples.AddSamplesRow(6, "Sample6", "SampleType2");
            oDataset.Samples.AddSamplesRow(7, "Sample7", "SampleType3");
            oDataset.Samples.AddSamplesRow(8, "Sample8", "SampleType4");

        }

        protected override void CreateChildControls()
        {
            PopulateDataset();
            oView = new DataView(oDataset.Samples);

            oGrid = new SPGridView();
            oGrid.DataSource = oView;
            oGrid.AutoGenerateColumns = false;
            oGrid.AllowSorting = true;
            oGrid.Sorting += new GridViewSortEventHandler(oGrid_Sorting);

            oGrid.RowDataBound += new GridViewRowEventHandler(oGrid_RowDataBound);

            BoundField colName = new BoundField();
            colName.DataField = "SampleName";
            colName.HeaderText = "Sample Name";
            colName.SortExpression = "SampleName";
            oGrid.Columns.Add(colName);

            // Add the menu control here

            BoundField colType = new BoundField();
            colType.DataField = "SampleType";
            colType.HeaderText = "Sample Type";
            colType.SortExpression = "SampleType";
            oGrid.Columns.Add(colType);

            // Replace the Name coloumn with a shiny menu
            colName.Visible = false;  // You could remove colName completely

            SPMenuField colMenu = new SPMenuField();
            colMenu.HeaderText = "Sample Name";
            colMenu.TextFields = "SampleName";
            colMenu.MenuTemplateId = "SampleListMenu";
            colMenu.NavigateUrlFields = "SampleID,SampleName";
            colMenu.NavigateUrlFormat = "do.aspx?p={0}&q={1}";
            colMenu.TokenNameAndValueFields = "EDIT=SampleID,NAME=SampleName";
            colMenu.SortExpression = "SampleName";

            MenuTemplate sampleListMenu = new MenuTemplate();
            sampleListMenu.ID = "SampleListMenu";
            MenuItemTemplate biogMenu = new MenuItemTemplate(
                "Sample Results Option", "/_layouts/images/EawfNewUser.gif");
            biogMenu.ClientOnClickNavigateUrl = "do.aspx?this=%EDIT%&that=%NAME%";
            //entry.ClientOnClickScript = "your javascript here";
            sampleListMenu.Controls.Add(biogMenu);

            MenuItemTemplate sampleTypeMenu = new MenuItemTemplate(
                    "Sample Type Option", "/_layouts/images/ICWM.gif");
            sampleListMenu.Controls.Add(sampleTypeMenu);

            MenuSeparatorTemplate sepMenu = new MenuSeparatorTemplate();
            sampleListMenu.Controls.Add(sepMenu);

            MenuItemTemplate favMenu = new MenuItemTemplate(
                   "Add to Favorites", "/_layouts/images/addtofavorites.gif");
            sampleListMenu.Controls.Add(favMenu);

            this.Controls.Add(sampleListMenu);
            oGrid.Columns.Add(colMenu);


            Controls.Add(oGrid);
            oGrid.DataBind();

            oTextBox.Visible = true;
            this.Controls.Add(oTextBox);

            base.CreateChildControls();

        }

        void oGrid_Sorting(object sender, GridViewSortEventArgs e)
        {
            string lastExpression = "";
            if (ViewState["SortExpression"] != null)
                lastExpression = ViewState["SortExpression"].ToString();

            string lastDirection = "asc";
            if (ViewState["SortDirection"] != null)
                lastDirection = ViewState["SortDirection"].ToString();

            string newDirection = "asc";
            if (e.SortExpression == lastExpression)
                newDirection = (lastDirection == "asc") ? "desc" : "asc";

            ViewState["SortExpression"] = e.SortExpression;
            ViewState["SortDirection"] = newDirection;

            oView.Sort = e.SortExpression + " " + newDirection;
            oGrid.DataBind();

        }

        void oGrid_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            // Runs for each row being bound to the data

            string messageText = string.Empty;

            // Show or hide the MenuControl  or label control depending on the Type

            try
            {
                foreach (Control control in e.Row.Controls)
                {
                    messageText = "ControlID: " + control.ID + "\r\n";
                    messageText += "Has Controls ?: " + control.HasControls().ToString() + "\r\n";
                    AddMessage(messageText);
                    foreach(Control child in control.Controls)
                    {
                        messageText = "-----ControlID: " + control.ID + "\r\n";
                        messageText += "-----Has Controls ?: " + control.HasControls().ToString() + "\r\n";
                        LiteralControl lcc = new LiteralControl("<DIV>" + messageText + "</DIV><BR>");
                        this.Controls.Add(lcc);
                    }
                }

            }
            catch (Exception ex)
            {
                messageText += ex.Message + "\r\n";
            }
        }

        void AddMessage(string Message)
        {
            LiteralControl lc = new LiteralControl("<DIV>" + Message + "</DIV><BR>");
            this.Controls.Add(lc);
        }
    }
}
